package com.dys.flex;

import com.dys.entity.SysUser;
import com.dys.mapper.SysUserMapper;
import com.dys.vo.UserVO;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.dys.entity.table.SysUserTableDef.SYS_USER;

/**
 * @Author: 疾风
 * @ClassName: InsertData
 * @Description: mybatis flex 插入数据
 * @CreateDate: 2025/01/13
 * @Version: v1.0.0
 */
@SpringBootTest
public class InsertData {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Test
    void testInsert(){
        // user_gender: 0:男，1:女，2:保密
        SysUser user = new SysUser();
        user.setNickName("测试人员2");
        user.setUserName("test2");
        user.setPassword("1234567");
        user.setStatus("1");
        // user_gender->null
        // 不带主键插入：
        // int row = sysUserMapper.insert(user);
        // user_gender->使用数据库的默认值
        int row = sysUserMapper.insertSelective(user);
        System.out.println("受影响行数："+row);
        System.out.println("主键："+user.getId());
    }

    @Test
    void testInsertWithPK(){
        // 插入时,设置主键
        // user_gender: 0:男，1:女，2:保密
        SysUser user = new SysUser();
        user.setId(11L);
        user.setNickName("测试人员3");
        user.setUserName("test3");
        user.setPassword("12345678");
        user.setStatus("1");
        // user_gender->null
        // 带主键插入：
        // int row = sysUserMapper.insertWithPk(user);
        // user_gender->使用数据库的默认值
        int row = sysUserMapper.insertSelectiveWithPk(user);
        System.out.println("受影响行数："+row);
        System.out.println("主键："+user.getId());
    }

    @Test
    void testInsertOrUpdate(){
        // 插入时,设置主键
        // user_gender: 0:男，1:女，2:保密
        SysUser user = new SysUser();
        user.setId(11L);
        user.setNickName("测试人员修改");
        user.setUserName("test03");
        user.setPassword("12345678");
        user.setStatus("1");
        // user_gender->null
        // 存在id,则执行修改,否则执行插入
        // int row = sysUserMapper.insertOrUpdate(user);
        // user_gender->使用数据库的默认值
        int row = sysUserMapper.insertOrUpdateSelective(user);
        // int row = sysUserMapper.insertOrUpdate(user,true);
        System.out.println("受影响行数："+row);
        System.out.println("主键："+user.getId());
    }

    /**
     * 批量插入
     */
    @Test
    void testInsertBatch(){
        List<SysUser> list = new ArrayList<>();
        SysUser user1 = new SysUser();
        user1.setNickName("测试人员4");
        user1.setUserName("test4");
        user1.setPassword("12345678");
        user1.setStatus("1");
        list.add(user1);
        SysUser user2 = new SysUser();
        user2.setNickName("测试人员5");
        user2.setUserName("test5");
        user2.setPassword("12345678");
        user2.setStatus("1");
        list.add(user2);
        // 不忽略null值
        int row = sysUserMapper.insertBatch(list);
        System.out.println("受影响行数："+row);
    }

    /**
     * 插入数据忽略null,比如userName=null,createTime->null
     */
    @Test
    void testInsertDataIgnoreNulls(){
        SysUser user = new SysUser();
        user.setNickName("测试人员");
        user.setUserName("test");
        user.setPassword("123456");
        user.setStatus("1");
        int row = sysUserMapper.insert(user, true);
        System.out.println("受影响行数："+row);
    }

    @Test
    void testWhere(){
        // where条件：byId where id = xxx
        // byMap: where id = 1
        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        // byCondition
        QueryCondition queryCondition = SYS_USER.ID.eq(1);
        // byQuery: 多用于查询
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(SYS_USER.ID.in(1, 2));
        List<UserVO> list = sysUserMapper.selectListByQueryAs(queryWrapper, UserVO.class);
        list.forEach(System.out::println);
    }
}
